﻿using Dt261.ble;
using GalaSoft.MvvmLight.Command;
using Panuon.UI.Silver;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Devices.Bluetooth;

namespace Dt261.ViewModel
{
    class PureToneCorrectViewModel : TestWindowViewModel
    {
        int toneVlaue;
        int mastValue;

        //~PureToneCorrectViewModel()
        //{
          
        //}

        public PureToneCorrectViewModel()
        {
            viewID = "02";



            DMSkin.Core.Broadcast.RegisterBroadcast<byte[]>("NEWCORRECTDATA", (obj) =>
            {


                
                byte[] dataBuffer = obj;
                Console.WriteLine("收到NEWCORRECTDATA：" + TLJConvert.byteToHexStr(dataBuffer));
                TLJCommand com = new TLJCommand(CommandAction.CommandAction_Reply);

                ArrayList list = new ArrayList();
                CommandInfo.parseWithHexData(dataBuffer, list);

              

                foreach (CommandInfo info in list)
                {
                    com.appendWithCommandInfo(info);
                }

                byte[] dd = com.getProtocolHex();
                bluetooth.Write(dd);

                toneVlaue = dataBuffer[2];
                mastValue = dataBuffer[5];

                ToneVlaue = dataBuffer[2];
                MastValue = dataBuffer[5];
            });


        }

        public int ToneVlaue { get => toneVlaue; set{

                int fix = value - toneVlaue;
                toneVlaue = value; RaisePropertyChanged();

                if(fix != 0)
                {
                    if (bluetooth != null && bluetooth.CurrentDevice != null && bluetooth.CurrentDevice.ConnectionStatus == BluetoothConnectionStatus.Connected)
                    {
                        ArrayList list = new ArrayList();
                        {
                            list.Add(toneVlaue.ToString("X2"));//
                        }

                        TLJCommand com = new TLJCommand(CommandType.CommandType_SetHeadphoneValue, CommandAction.CommandAction_Set, list);
                        byte[] dd = com.getProtocolHex();
                        bluetooth.Write(dd);
                    }

                    if (this.IsTracking)
                    {
                        MastValue += fix;
                    }
                }
 
            }
        }
        public int MastValue { get => mastValue; set {

                int fix = value - mastValue;
                mastValue = value; RaisePropertyChanged();

                if(fix != 0)
                {
                    if (bluetooth != null && bluetooth.CurrentDevice != null && bluetooth.CurrentDevice.ConnectionStatus == BluetoothConnectionStatus.Connected)
                    {
                        ArrayList list = new ArrayList();
                        {
                            list.Add(mastValue.ToString("X2"));//
                        }

                        TLJCommand com = new TLJCommand(CommandType.CommandType_SetCoverValue, CommandAction.CommandAction_Set, list);
                        byte[] dd = com.getProtocolHex();
                        bluetooth.Write(dd);
                    }
                }

            }
        }


        private RelayCommand<string> savetResultCommand;

        public RelayCommand<string> SaveResultCommand
        {
            get
            {
                if (savetResultCommand == null)
                {
                    savetResultCommand = new RelayCommand<string>((noreps) => saveResult());
                }
                return savetResultCommand;
            }
        }



        public void saveResult()
        {
            if (bluetooth != null && bluetooth.CurrentDevice != null && bluetooth.CurrentDevice.ConnectionStatus == BluetoothConnectionStatus.Connected)
            {
                ArrayList list = new ArrayList();
                {
                    list.Add("01");//
                }

                TLJCommand com = new TLJCommand(CommandType.CommandType_SaveValue, CommandAction.CommandAction_Set, list);
                byte[] dd = com.getProtocolHex();
                bluetooth.Write(dd);
            }

            Notice.Show("校准结果已保存.", "提示", 3, MessageBoxIcon.Success);

            //DMSkin.Core.Broadcast.PushBroadcast("NavigationBack2", "TestWindowViewModel");

            //DMSkin.Core.Broadcast.PushBroadcast("CLOSEWINDOW", "TestWindowViewModel");
        }
    }
}
